﻿using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Listing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Page.Form.DefaultFocus = placeBidTextBox.ClientID;
       Page.Form.DefaultButton = bidButton.UniqueID;

        //check to ensure the user is logged in
        if (Session["userID"] == null)
        { Response.Redirect("Default.aspx"); }


        if (Session["AccountType"].Equals("S"))
        {
            placeBidTextBox.Visible = false;
            howMuchBidLbl.Visible = false;
            buyNowButton.Visible = false;
            bidButton.Visible = false;
        }

        OleDbConnection conn = null;
        OleDbDataReader reader = null;
        string AuctionID = (Request.QueryString["AuctionID"]);

        //Set a table width.
        displayTable.Width = Unit.Percentage(30.00);

        conn = new OleDbConnection(
            "Provider=Microsoft.ACE.OLEDB.12.0; " +
            "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
        conn.Open();

        OleDbCommand cmd = new OleDbCommand("SELECT * FROM Auctions WHERE ID=" + AuctionID, conn);
        reader = cmd.ExecuteReader();

        reader.Read();

        //Title Row
        TableRow titleRow = new TableRow();
        TableHeaderCell titleTextCell = new TableHeaderCell();
        titleTextCell.Text = "Title:";
        titleTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell titleDataCell = new TableCell();
        titleDataCell.Text = reader["title"].ToString();
        titleRow.Cells.Add(titleTextCell);
        titleRow.Cells.Add(titleDataCell);

        //Seller Row
        TableRow sellerRow = new TableRow();
        TableHeaderCell sellerTextCell = new TableHeaderCell();
        sellerTextCell.Text = "Seller:";
        sellerTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell sellerDataCell = new TableCell();
        sellerDataCell.Text = reader["seller"].ToString();
        sellerRow.Cells.Add(sellerTextCell);
        sellerRow.Cells.Add(sellerDataCell);

        //Current High Bid Row
        TableRow currentBidRow = new TableRow();
        TableHeaderCell currentBidTextCell = new TableHeaderCell();
        currentBidTextCell.Text = "Current Bid:";
        currentBidTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell currentBidDataCell = new TableCell();
        currentBidDataCell.Text = "$" + reader["currentBid"].ToString();
        currentBidRow.Cells.Add(currentBidTextCell);
        currentBidRow.Cells.Add(currentBidDataCell);

        //Current Winner Row
        TableRow currentWinnerRow = new TableRow();
        TableHeaderCell currentWinnerTextCell = new TableHeaderCell();
        currentWinnerTextCell.Text = "Current Winner:";
        currentWinnerTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell currentWinnerDataCell = new TableCell();
        currentWinnerDataCell.Text = reader["currentWinner"].ToString();
        currentWinnerRow.Cells.Add(currentWinnerTextCell);
        currentWinnerRow.Cells.Add(currentWinnerDataCell);

        //Number of Bids Row
        TableRow numberOfBidsRow = new TableRow();
        TableHeaderCell numberOfBidsTextCell = new TableHeaderCell();
        numberOfBidsTextCell.Text = "Number Of Bids:";
        numberOfBidsTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell numberOfBidsDataCell = new TableCell();
        numberOfBidsDataCell.Text = reader["numberOfBids"].ToString();
        numberOfBidsRow.Cells.Add(numberOfBidsTextCell);
        numberOfBidsRow.Cells.Add(numberOfBidsDataCell);

        //Buy Now Price Row
        TableRow buyNowPriceRow = new TableRow();
        TableHeaderCell buyNowPriceTextCell = new TableHeaderCell();
        buyNowPriceTextCell.Text = "Buy Now Price:";
        buyNowPriceTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell buyNowPriceDataCell = new TableCell();
        buyNowPriceDataCell.Text = "$" + reader["buyNowPrice"].ToString();
        buyNowPriceRow.Cells.Add(buyNowPriceTextCell);
        buyNowPriceRow.Cells.Add(buyNowPriceDataCell);

        //Close the reader
        reader.Close();

        //Close the DB connection
        conn.Close();

        displayTable.Rows.Add(titleRow);
        displayTable.Rows.Add(sellerRow);
        displayTable.Rows.Add(currentBidRow);
        displayTable.Rows.Add(currentWinnerRow);
        displayTable.Rows.Add(numberOfBidsRow);
        displayTable.Rows.Add(buyNowPriceRow);        
    }
    protected void cmdBuyNow(object sender, EventArgs e)
    {
        int AuctionID = int.Parse(Request.QueryString["AuctionID"]);
        Response.Redirect("Checkout.aspx?AuctionID=" + AuctionID + "&BuyNow=Yes");    
    }

    protected void cmdBid(object sender, EventArgs e)
    {
        int currentBid = 0;
        int minimumBid = 0;
        int numberOfBids = 0;
        int newBid = 0;
        int AuctionID = int.Parse(Request.QueryString["AuctionID"]);
        string dbUpdate = null;

        if (placeBidTextBox.Text == "")
        {
            //Display message notifying user that thier bid was successful
            string ScriptString1 = "alert('You must enter a Dollar amount in the Bid box.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }
        else
        {
            newBid = Convert.ToInt32(placeBidTextBox.Text);
        }

        OleDbConnection conn = null;
        OleDbDataReader reader = null;
                
        conn = new OleDbConnection(
            "Provider=Microsoft.ACE.OLEDB.12.0; " +
            "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
        conn.Open();

        //SQL statement to read data from the DB
        OleDbCommand cmdRead = new OleDbCommand("SELECT * FROM Auctions WHERE ID=" + AuctionID, conn);
        reader = cmdRead.ExecuteReader();

        reader.Read();
        currentBid = int.Parse(reader["currentBid"].ToString());
        minimumBid = int.Parse(reader["minimumBid"].ToString());
        numberOfBids = int.Parse(reader["numberOfBids"].ToString());

        //Close the reader
        reader.Close();        
        
        if (currentBid == 0)
        {
            if (newBid <= minimumBid)
            {
                //Display message notifying user that their bid is lower than the minimum bid
                string ScriptString2 = "alert('Your Bid must be greater than the minimum bid');";
                System.Web.UI.Page page2 = HttpContext.Current.Handler as System.Web.UI.Page;
                page2.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString2, true);

            }
            else
            {
                currentBid = newBid;
                numberOfBids += 1;
            }
        }
        else
        {
            if (newBid <= currentBid)
            {
                //Display message notifying user that their bid is lower than the minimum bid
                string ScriptString2 = "alert('Your bid must be greater than the current bid');";
                System.Web.UI.Page page2 = HttpContext.Current.Handler as System.Web.UI.Page;
                page2.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString2, true);

            }
            else
            {
                currentBid = newBid;
                numberOfBids += 1;
            }

        }

        //Build the Update string
        dbUpdate =  "UPDATE Auctions SET ";
        dbUpdate += "currentWinner=@currentWinner, numberOfBids=@numberOfBids, currentBid=@currentBid ";        
        dbUpdate += "WHERE ID=@AuctionID";

        //Create the Write command
        OleDbCommand cmdWrite = new OleDbCommand(dbUpdate, conn);

        //Add the Parameters
        cmdWrite.Parameters.AddWithValue("@currentWinner", Session["userID"]);
        cmdWrite.Parameters.AddWithValue("@numberOfBids", numberOfBids);
        cmdWrite.Parameters.AddWithValue("@currentBid", currentBid);
        cmdWrite.Parameters.AddWithValue("@AuctionID", AuctionID);
        
        
        
        //Write the new data to the DB
        cmdWrite.ExecuteNonQuery();
        
        //Close the DB connection
        conn.Close();
        
        //Display message notifying user that their bid was successful
        string ScriptString = "alert('Your Bid was successful!');";
        ScriptString += "window.location='Default.aspx';";
        System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
        page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
    }

    protected void emailWinner(object sender, EventArgs e)
    {
    }

    protected void exitBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}